import {PluginCommand} from "../Command/PluginCommand";
import {ElementType} from "../modeling/TypeDefine";
import {Direction, Point} from "../../nodeApp/static/lib/crowncad-plugin-sdk-0.0.1";

class WingBeamCommand extends PluginCommand {
    constructor() {
        super();
        this.solidIndex = 0;
        this.planeIndex = 0;
        this.copyFaceIndex = 0;
        this.trimSurfaceIndex = 0;
        this.transformSurfaceIndex = 0;
        this.extrudeFaceIndex = 0;
        this.knitSurfaceIndex = 0;
        this.featrueName = "";
    }

    // 创建梁，输入条件有基准面、上下翼面，基准方向直线及相关的参数
    async createWingBeamPart(params) {
        // 创建定位及参数大小的基准面
        let planesRet = await this.createFaceBothSidesPlanebyBasePlane(params.basePlaneId, -params.flangeWidth / 2, params.flangeWidth / 2, this.generateFeatrueName("plane"), this.generateFeatrueName("plane"));

        /* 1、梁上缘条处理成实体 */
        let topFlangeSolid = {};
        if (planesRet.code === 200) {
            let resCreateSolid = await this.createTopFlangeSolid(params, planesRet);
            topFlangeSolid.entityId = resCreateSolid?.entityId;
            topFlangeSolid.entityName = resCreateSolid?.entityName;
        }

        /* 2、梁下缘条处理成实体 */
        let bottomFlangeSolid = {};
        if (planesRet.code === 200) {
            let resCreateSolid = await this.createBottomFlangeSolid(params, planesRet);
            bottomFlangeSolid.entityId = resCreateSolid?.entityId;
            bottomFlangeSolid.entityName = resCreateSolid?.entityName;
        }

        /* 3、梁腹板处理成实体 */
        // 创建基准面
        let platePlanesRes = await this.createFaceBothSidesPlanebyBasePlane(params.basePlaneId, -params.plateThickness / 2, params.plateThickness / 2, this.generateFeatrueName("plane"), this.generateFeatrueName("plane"));
        // 创建实体
        let plateSolid = {};
        if (platePlanesRes.code === 200) {
            let resCreateSolid = await this.createPlateSolid(params, platePlanesRes);
            plateSolid.entityId = resCreateSolid?.entityId;
            plateSolid.entityName = resCreateSolid?.entityName;
        }

        /* 4、铆接板处理成实体 */
        let rivetingPlateSolid = {};
        let rivetingPlateRes = await this.createRivetingPlateSolid(params);

        if (topFlangeSolid?.entityId > 0 &&
            bottomFlangeSolid?.entityId > 0 &&
            plateSolid?.entityId > 0 &&
            rivetingPlateRes === 4) { //4个铆接板
            this.eventBusAlertMsg("翼梁创建成功！", "success");
            return true;
        } else {
            this.eventBusAlertMsg("翼梁创建过程出现错误！", "error");
            return false;
        }
    }

    /**
     * 根据定位及参考参数创建上缘条实体
     * @param params 创建参考的参数
     * @param refPalnes 参考基准面
     * @returns {Promise<void>} 返回创建出来的体
     */
    async createTopFlangeSolid(params, refPlanes) {
        let createParams = {
            faceId: params.topFaceId,
            faceEntityId: params.topFaceRefEntityId,
            transformDistance: params.flangeThickness,
            verticalLinePoint: params.verticalLinePoint,
            reverse: 0
        };
        let featureNames = {
            featureCopyFaceName: this.generateFeatrueName("copy"),
            featureTrimSurfaceName: this.generateFeatrueName("trim"),
            featureTransformFaceName: this.generateFeatrueName("transform"),
            featureExtrudeFaceName: this.generateFeatrueName("extrude"),
            featureKnitSurfaceName: this.generateFeatrueName("knit")
        };
        let createSolidRet = await this.createSolidByFace(createParams, refPlanes, featureNames, true);
        return createSolidRet;
    }

    /**
     * 根据定位及参考参数创建下缘条实体
     * @param params 创建参考的参数
     * @param refPalnes 参考基准面
     * @returns {Promise<void>} 返回创建出来的体
     */
    async createBottomFlangeSolid(params, refPlanes) {
        let createParams = {
            faceId: params.bottomFaceId,
            faceEntityId: params.bottomFaceRefEntityId,
            transformDistance: params.flangeThickness,
            verticalLinePoint: params.verticalLinePoint,
            reverse: 1
        };
        let featureNames = {
            featureCopyFaceName: this.generateFeatrueName("copy"),
            featureTrimSurfaceName: this.generateFeatrueName("trim"),
            featureTransformFaceName: this.generateFeatrueName("transform"),
            featureExtrudeFaceName: this.generateFeatrueName("extrude"),
            featureKnitSurfaceName: this.generateFeatrueName("knit")
        };
        let createSolidRet = await this.createSolidByFace(createParams, refPlanes, featureNames, true);
        return createSolidRet;
    }

    /**
     * 创建腹板
     * @param params
     * @param refPlanes
     * @returns {Promise<void>}
     */
    async createPlateSolid(params, refPlanes) {
        let plateSolidRet = {};

        // 1、复制上面创建：CopyTopFaceUsedPlate
        //let copyTopRet = await this.copySurface(params.topFaceId, params.topFaceRefEntityId, "CopyTopFaceUsedPlate");
        // 偏移复制上面
        let copyTopRet = await this.transformCopySurface(params?.topFaceRefEntityId, params?.flangeThickness, params?.verticalLinePoint, 0, this.generateFeatrueName("copy"));

        // 2、裁剪上面生成可使用的面
        let limitIds = [];
        limitIds.push(refPlanes.leftPlaneId);
        limitIds.push(refPlanes.rightPlaneId);

        let topSurfaceId = 0;
        if (copyTopRet?.code === 200) {
            topSurfaceId = copyTopRet?.elementId;
        }

        let trimTopSurfaceName = this.generateFeatrueName("trim");
        let trimTopSurfaceRet = await this.trimSurface(topSurfaceId, limitIds, trimTopSurfaceName);

        // 3、复制下面创建：CopyBottomFaceUsedPlate
        //let copyBottomRet = await this.copySurface(params.bottomFaceId, params.bottomFaceRefEntityId, "CopyBottomFaceUsedPlate");
        // 偏移复制上面
        let copyBottomRet = await this.transformCopySurface(params?.bottomFaceRefEntityId, params?.flangeThickness, params?.verticalLinePoint, 1, this.generateFeatrueName("copy"));

        // 4、裁剪下面生成可使用的面
        // 使用2中相同的limitIds
        let bottomSurfaceId = 0;
        if (copyBottomRet?.code === 200) {
            bottomSurfaceId = copyBottomRet?.elementId;
        }

        let trimBottomSurfaceName = this.generateFeatrueName("trim");
        let trimBottomSurfaceRet = await this.trimSurface(bottomSurfaceId, limitIds, trimBottomSurfaceName);

        // 5、根据上下面信息，进行拉伸
        let extrudePlate = await this.extrudeSurface(trimTopSurfaceRet?.elementId, trimBottomSurfaceRet?.elementId, params?.verticalLinePoint, 0, this.generateFeatrueName("extrude"));

        // 6、根据上面、下面、拉伸面进行缝合成体
        if (extrudePlate?.code === 200) {
            let entityIds = [trimTopSurfaceRet?.entityId, trimBottomSurfaceRet?.entityId];
            extrudePlate?.faces.forEach(ent => {
                entityIds.push(ent?.entityId);
            });
            let knitPlateRet = await this.knitSurface(entityIds, this.generateFeatrueName("knit"));
            if (knitPlateRet?.code === 200) {
                plateSolidRet.entityId = knitPlateRet?.entityId;
                plateSolidRet.entityName = knitPlateRet?.entityName;
                plateSolidRet.code = knitPlateRet?.code;
                console.log("腹板创建成功");
            } else {
                console.log("腹板创建过程出现错误");
            }
        }
        return plateSolidRet;
    }

    // 创建铆接板
    async createRivetingPlateSolid(params) {
        let rivetingNum = 0;
        // 用于边缘板较宽的截取距离
        let rightUsedByFlangeEdgeDistance1 = params.plateThickness / 2;
        let rightUsedByFlangeEdgeDistance2 = params.flangeWidth / 2;
        let rightUsedByEdgePlanesRes = await this.createFaceBothSidesPlanebyBasePlane(params.basePlaneId, rightUsedByFlangeEdgeDistance1, rightUsedByFlangeEdgeDistance2, this.generateFeatrueName("plane"), this.generateFeatrueName("plane"));
        console.log("右侧基准面第一次创建结束");

        // 用于边缘板贴合腹板的体的截取距离
        let rightUsedByFlangePlateDistance1 = params.plateThickness / 2;
        let rightUsedByFlangePlateDistance2 = params.plateThickness / 2 + params.rivetingThickness;
        let rightUsedByFlangePlatePlanesRes = await this.createFaceBothSidesPlanebyBasePlane(params.basePlaneId, rightUsedByFlangePlateDistance1, rightUsedByFlangePlateDistance2, this.generateFeatrueName("plane"), this.generateFeatrueName("plane"));
        console.log("右侧基准面第二次创建结束");

        /* 1.右上侧铆接板*/
        // 创建实体
        let rivetingHorRTSolid = {};
        // 创建实体,宽的部分
        if (rightUsedByEdgePlanesRes.code === 200) {
            let solidCreateRet = await this.createSingleRivetingSolid(params?.topFaceId,
                params?.topFaceRefEntityId,
                rightUsedByEdgePlanesRes,
                params?.verticalLinePoint,
                0,
                params?.flangeThickness,
                params?.flangeThickness
            );
            if (solidCreateRet?.code === 200) {
                rivetingHorRTSolid.entityId = solidCreateRet?.entityId;
                rivetingHorRTSolid.entityName = solidCreateRet?.entityName;
                rivetingHorRTSolid.code = solidCreateRet?.code;
            }
        }

        // 创建实体，窄的部分
        let rivetingVerRTSolid = {};
        if (rightUsedByFlangePlatePlanesRes.code === 200) {
            let solidCreateRet = await this.createSingleRivetingSolid(params?.topFaceId,
                params?.topFaceRefEntityId,
                rightUsedByFlangePlatePlanesRes,
                params?.verticalLinePoint,
                0,
                params?.flangeThickness,
                params?.rivetingHeight
            );
            if (solidCreateRet?.code === 200) {
                rivetingVerRTSolid.entityId = solidCreateRet?.entityId;
                rivetingVerRTSolid.entityName = solidCreateRet?.entityName;
                rivetingVerRTSolid.code = solidCreateRet?.code;
            }
        }
        //求和
        if (rivetingHorRTSolid?.code === 200 && rivetingVerRTSolid?.code === 200) {
            let unionSolidRet = await this.booleanUnion([rivetingHorRTSolid?.entityId, rivetingVerRTSolid?.entityId], this.generateFeatrueName("solid"));
            if (unionSolidRet?.code === 200) {
                rivetingNum++;
                console.log("右上侧铆接板创建成功");
            }
        }

        /* 2.右下侧铆接板*/
        // 创建实体
        let rivetingHorRBSolid = {};
        // 创建实体,宽的部分
        if (rightUsedByEdgePlanesRes.code === 200) {
            let solidCreateRet = await this.createSingleRivetingSolid(params?.bottomFaceId,
                params?.bottomFaceRefEntityId,
                rightUsedByEdgePlanesRes,
                params?.verticalLinePoint,
                1,
                params?.flangeThickness,
                params?.flangeThickness
            );
            if (solidCreateRet?.code === 200) {
                rivetingHorRBSolid.entityId = solidCreateRet?.entityId;
                rivetingHorRBSolid.entityName = solidCreateRet?.entityName;
                rivetingHorRBSolid.code = solidCreateRet?.code;
            }
        }

        // 创建实体，窄的部分
        let rivetingVerRBSolid = {};
        if (rightUsedByFlangePlatePlanesRes.code === 200) {
            let solidCreateRet = await this.createSingleRivetingSolid(params?.bottomFaceId,
                params?.bottomFaceRefEntityId,
                rightUsedByFlangePlatePlanesRes,
                params?.verticalLinePoint,
                1,
                params?.flangeThickness,
                params?.rivetingHeight
            );
            if (solidCreateRet?.code === 200) {
                rivetingVerRBSolid.entityId = solidCreateRet?.entityId;
                rivetingVerRBSolid.entityName = solidCreateRet?.entityName;
                rivetingVerRBSolid.code = solidCreateRet?.code;
            }
        }
        //求和
        if (rivetingHorRBSolid?.code === 200 && rivetingVerRBSolid?.code === 200) {
            let unionSolidRet = await this.booleanUnion([rivetingHorRBSolid?.entityId, rivetingVerRBSolid?.entityId], this.generateFeatrueName("solid"));
            if (unionSolidRet?.code === 200) {
                rivetingNum++;
                console.log("右下侧铆接板创建成功");
            }
        }

        //--用于左侧分割的基准面--
        // 用于边缘板较宽的截取距离
        let leftUsedByFlangeEdgeDistance1 = -params.flangeWidth / 2;
        let leftUsedByFlangeEdgeDistance2 = -params.plateThickness / 2;
        let leftUsedByEdgePlanesRes = await this.createFaceBothSidesPlanebyBasePlane(params.basePlaneId, leftUsedByFlangeEdgeDistance1, leftUsedByFlangeEdgeDistance2, this.generateFeatrueName("plane"), this.generateFeatrueName("plane"));
        console.log("左侧基准面第一次创建结束");

        // 用于边缘板贴合腹板的体的截取距离
        let leftUsedByFlangePlateDistance1 = -(params.plateThickness / 2 + params.rivetingThickness);
        let leftUsedByFlangePlateDistance2 = -params.plateThickness / 2;
        let leftUsedByFlangePlatePlanesRes = await this.createFaceBothSidesPlanebyBasePlane(params.basePlaneId, leftUsedByFlangePlateDistance1, leftUsedByFlangePlateDistance2, this.generateFeatrueName("plane"), this.generateFeatrueName("plane"));
        console.log("左侧基准面第二次创建结束");

        /* 3.左上侧铆接板*/
        // 创建实体
        let rivetingHorLTSolid = {};
        // 创建实体,宽的部分
        if (leftUsedByEdgePlanesRes.code === 200) {
            let solidCreateRet = await this.createSingleRivetingSolid(params?.topFaceId,
                params?.topFaceRefEntityId,
                leftUsedByEdgePlanesRes,
                params?.verticalLinePoint,
                0,
                params?.flangeThickness,
                params?.flangeThickness
            );
            if (solidCreateRet?.code === 200) {
                rivetingHorLTSolid.entityId = solidCreateRet?.entityId;
                rivetingHorLTSolid.entityName = solidCreateRet?.entityName;
                rivetingHorLTSolid.code = solidCreateRet?.code;
            }
        }

        // 创建实体，窄的部分
        let rivetingVerLTSolid = {};
        if (leftUsedByFlangePlatePlanesRes.code === 200) {
            let solidCreateRet = await this.createSingleRivetingSolid(params?.topFaceId,
                params?.topFaceRefEntityId,
                leftUsedByFlangePlatePlanesRes,
                params?.verticalLinePoint,
                0,
                params?.flangeThickness,
                params?.rivetingHeight
            );
            if (solidCreateRet?.code === 200) {
                rivetingVerLTSolid.entityId = solidCreateRet?.entityId;
                rivetingVerLTSolid.entityName = solidCreateRet?.entityName;
                rivetingVerLTSolid.code = solidCreateRet?.code;
            }
        }
        //求和
        if (rivetingHorLTSolid?.code === 200 && rivetingVerLTSolid?.code === 200) {
            let unionSolidRet = await this.booleanUnion([rivetingHorLTSolid?.entityId, rivetingVerLTSolid?.entityId], this.generateFeatrueName("solid"));
            if (unionSolidRet?.code === 200) {
                rivetingNum++;
                console.log("左上侧铆接板创建成功");
            }
        }

        /* 4.左下侧铆接板*/
        // 创建实体
        let rivetingHorLBSolid = {};
        // 创建实体,宽的部分
        if (leftUsedByEdgePlanesRes.code === 200) {
            let solidCreateRet = await this.createSingleRivetingSolid(params?.bottomFaceId,
                params?.bottomFaceRefEntityId,
                leftUsedByEdgePlanesRes,
                params?.verticalLinePoint,
                1,
                params?.flangeThickness,
                params?.flangeThickness
            );
            if (solidCreateRet?.code === 200) {
                rivetingHorLBSolid.entityId = solidCreateRet?.entityId;
                rivetingHorLBSolid.entityName = solidCreateRet?.entityName;
                rivetingHorLBSolid.code = solidCreateRet?.code;
            }
        }

        // 创建实体，窄的部分
        let rivetingVerLBSolid = {};
        if (leftUsedByFlangePlatePlanesRes.code === 200) {
            let solidCreateRet = await this.createSingleRivetingSolid(params?.bottomFaceId,
                params?.bottomFaceRefEntityId,
                leftUsedByFlangePlatePlanesRes,
                params?.verticalLinePoint,
                1,
                params?.flangeThickness,
                params?.rivetingHeight
            );
            if (solidCreateRet?.code === 200) {
                rivetingVerLBSolid.entityId = solidCreateRet?.entityId;
                rivetingVerLBSolid.entityName = solidCreateRet?.entityName;
                rivetingVerLBSolid.code = solidCreateRet?.code;
            }
        }
        //求和
        if (rivetingHorLBSolid?.code === 200 && rivetingVerLBSolid?.code === 200) {
            let unionSolidRet = await this.booleanUnion([rivetingHorLBSolid?.entityId, rivetingVerLBSolid?.entityId], this.generateFeatrueName("solid"));
            if (unionSolidRet?.code === 200) {
                rivetingNum++;
                console.log("右下侧铆接板创建成功");
            }
        }
        return rivetingNum;
    }

    /**
     * 创建单个实体
     * @param faceId 参考面elementId
     * @param faceEntityRefId 参考面所属entityId
     * @param refPlanes 参考的左右两个基准面
     * @param directionPoint 用于方向控制的起始点
     * @param reverse 是否反向
     * @param copyDistance 复制面时方向上的距离
     * @param transformDistance 变换面复制时方向上的距离
     * @returns {Promise<{}>}
     */
    async createSingleRivetingSolid(faceId, faceEntityRefId, refPlanes, directionPoint, reverse, copyDistance, transformDistance) {
        let singleSolid = {};
        let transformCopyHorRet = await this.transformCopySurface(faceEntityRefId, copyDistance, directionPoint, reverse, this.generateFeatrueName("transform"));
        let featureNames = {
            featureCopyFaceName: this.generateFeatrueName("copy"),
            featureTrimSurfaceName: this.generateFeatrueName("trim"),
            featureTransformFaceName: this.generateFeatrueName("transform"),
            featureExtrudeFaceName: this.generateFeatrueName("extrude"),
            featureKnitSurfaceName: this.generateFeatrueName("knit")
        };

        if (transformCopyHorRet?.code === 200) {
            let params = {
                faceId: transformCopyHorRet?.elementId,
                faceEntityId: transformCopyHorRet?.entityId,
                transformDistance: transformDistance,
                verticalLinePoint: directionPoint,
                reverse: reverse
            };

            let createSolidRet = await this.createSolidByFace(params, refPlanes, featureNames, false);
            if (createSolidRet?.code === 200) {
                singleSolid.entityId = createSolidRet?.entityId;
                singleSolid.entityName = createSolidRet?.entityName;
                singleSolid.code = createSolidRet?.code;
            }
        }
        return singleSolid;
    }

    /**
     * 根据定位基准面创建目标面两侧的基准面
     * @param basePlaneId 参考基准面
     * @param distance1 间距-左
     * @param distance2 间距-右
     * @param leftPlaneName 左侧创建的特征名
     * @param rightPlaneName 右侧创建的特征名
     * @returns {Promise<{}>} 返回planes的信息，包含id、name、type、code等，code为200说明正常创建
     */
    async createFaceBothSidesPlanebyBasePlane(basePlaneId, distance1, distance2, leftPlaneName, rightPlaneName) {
        let resPlanes = {};
        let dis1 = 0;
        let reverse1 = 0;
        if (distance1 >= 0) {
            dis1 = distance1;
            reverse1 = 0;
        } else {
            dis1 = -distance1;
            reverse1 = 1;
        }
        let leftPlaneParams = {
            planeId: basePlaneId,
            paralDis: dis1,
            reverse: reverse1
        };

        let dis2 = 0;
        let reverse2 = 0;
        if (distance2 >= 0) {
            dis2 = distance2;
            reverse2 = 0;
        } else {
            dis2 = -distance2;
            reverse2 = 1;
        }
        let rightPlaneParams = {
            planeId: basePlaneId,
            paralDis: dis2,
            reverse: reverse2
        };

        let resLeftPlane = await this.command.datum.createPlaneByDistanceIncrement(leftPlaneParams, leftPlaneName);
        let resRightPlane = await this.command.datum.createPlaneByDistanceIncrement(rightPlaneParams, rightPlaneName);

        if (resLeftPlane?.code === 200 && resRightPlane?.code === 200) {
            let leftCurrentFeature = resLeftPlane?.data?.modelData?.currentFeature;
            let rightCurrentFeature = resRightPlane?.data?.modelData?.currentFeature;
            if (leftCurrentFeature?.length > 0) {
                let entityId = await this.query.getElementsByFeatureId(leftCurrentFeature[0].id);
                if (entityId?.code === 200) {
                    let elements = entityId?.data?.elements;
                    elements.forEach(ele => {
                        if (ele.type === ElementType.DATUMPLANE) {
                            resPlanes.leftPlaneId = ele?.id;
                            resPlanes.leftPlaneType = ele?.type;
                        }
                    });
                    resPlanes.leftPlaneName = leftCurrentFeature[0].name;
                }
            } else {
                this.eventBusAlertMsg("左侧基准面创建出现异常", "warning");
            }

            if (rightCurrentFeature?.length > 0) {
                let entityId = await this.query.getElementsByFeatureId(rightCurrentFeature[0].id);
                if (entityId?.code === 200) {
                    let elements = entityId?.data?.elements;
                    elements.forEach(ele => {
                        if (ele.type === ElementType.DATUMPLANE) {
                            resPlanes.rightPlaneId = ele?.id;
                            resPlanes.rightPlaneType = ele?.type;
                        }
                    });
                    resPlanes.rightPlaneName = rightCurrentFeature[0].name;
                }
            } else {
                this.eventBusAlertMsg("右侧基准面创建出现异常", "warning");
            }
        } else {
            this.eventBusAlertMsg("两侧创建基准面创建出现异常", "warning");
        }

        // 加状态码，正常创建为200，否则为-1
        if (resPlanes?.leftPlaneId > 0 && resPlanes.rightPlaneId > 0) {
            resPlanes.code = 200;
        } else {
            resPlanes.code = -1;
        }
        return resPlanes;
    }

    /**
     * 根据参考曲面创建体
     * @param params 曲面创建的参考信息，包括faceId/faceEntityId/transformDistance/verticalLinePoint/reverse等
     * @param refPlanes 参考的分割基准面，包括leftPlaneId/rightPlaneId等
     * @param featureNames 创建过程中的特征名，包括featureCopyFaceName/featureTrimSurfaceName/featureTransformFaceName/featureExtrudeFaceName/featureKnitSurfaceName等
     * @param needCopy 是否需要复制面操作
     * @returns {Promise<{}>}
     */
    async createSolidByFace(params, refPlanes, featureNames, needCopy) {
        let solidRet = {};
        // 1、复制面
        let resCopy = {};
        if (needCopy) {
            resCopy = await this.copySurface(params?.faceId, params?.faceEntityId, featureNames?.featureCopyFaceName);
        } else { //直接使用面进行操作
            resCopy.code = 200;
            resCopy.elementId = params?.faceId;
        }

        // 2、裁剪面生成可使用的面
        let limitIds = [];
        limitIds.push(refPlanes.leftPlaneId);
        limitIds.push(refPlanes.rightPlaneId);
        let surfaceId = 0;
        if (resCopy?.code === 200) {
            surfaceId = resCopy?.elementId;
        }
        let trimSurfaceRet = await this.trimSurface(surfaceId, limitIds, featureNames?.featureTrimSurfaceName);
        //3、变换位置复制、拉伸、缝合处理成实体
        //3.1变换复制面
        let transformCopyRet = await this.transformCopySurface(trimSurfaceRet?.entityId, params?.transformDistance, params?.verticalLinePoint, params?.reverse, featureNames?.featureTransformFaceName);
        //3.2拉伸到面
        if (transformCopyRet?.code === 200) {
            let extrudeFlange = await this.extrudeSurface(trimSurfaceRet?.elementId, transformCopyRet.elementId, params?.verticalLinePoint, params?.reverse, featureNames?.featureExtrudeFaceName);
            if (extrudeFlange?.code === 200) {
                //3.3缝合面成体
                let entities = [trimSurfaceRet?.entityId, transformCopyRet?.entityId];
                extrudeFlange?.faces?.forEach(face => {
                    entities.push(face?.entityId);
                });
                let knitFlange = await this.knitSurface(entities, featureNames?.featureKnitSurfaceName);
                if (knitFlange?.code === 200) {
                    solidRet.entityId = knitFlange?.entityId;
                    solidRet.entityName = knitFlange?.entityName;
                    solidRet.code = knitFlange?.code;
                    console.log(featureNames?.featureKnitSurfaceName + "创建成功");
                    return solidRet;
                } else {
                    console.log(featureNames?.featureKnitSurfaceName + "创建出现错误");
                }
            }
        }
        return solidRet;
    }

    /**
     * 复制曲面
     * @param faceId 面ID
     * @param faceRefEntityId 面所属Entity的ID
     * @param featureName 创建面的特征名
     * @returns {Promise<boolean>} 返回创建面的信息，包含id、name、code等信息
     */
    async copySurface(faceId, faceRefEntityId, featureName) {
        let faceRet = {};
        let params = {
            surfaceIds: [faceId],
            instanceIds: [faceRefEntityId]
        };
        let copyFaceRet = await this.command.surface.copySurfaceIncrement(params, featureName);
        if (copyFaceRet?.code === 200 && copyFaceRet?.data?.modelData?.currentFeature?.length > 0) {
            let currentFeature = copyFaceRet?.data?.modelData?.currentFeature[0];
            let entityId = await this.query.getElementsByFeatureId(currentFeature.id);
            if (entityId?.code === 200) {
                let elements = entityId?.data?.elements;
                elements.forEach(ele => {
                    if (ele.type === ElementType.FACE) {
                        faceRet.elementId = ele.id;
                    }
                });
                faceRet.elementName = currentFeature.name;
            }
        }
        faceRet.code = copyFaceRet?.code;
        return faceRet;
    }

    /**
     * 裁剪面形成新的面
     * @param surfaceId 原始面id
     * @param limitIds 裁剪工具面id数组
     * @param trimSurfaceName 裁剪后特征名
     * @returns {Promise<{}>} 返回裁剪后的面信息，包含面id、name、type、code以及entity的信息等信息
     */
    async trimSurface(surfaceId, limitIds, trimSurfaceName) {
        let faceRet = {};
        let keepTags = [0, 1, 0]; // 保留中间
        let keepMode = 1; // 默认删除
        let params = {
            limitIds: limitIds,
            surfaceIds: [surfaceId],
            keepMode: keepMode,
            keepTags: keepTags,
            oldSurfaceIds: [surfaceId, surfaceId, surfaceId],
            newSurfaceIds: [0, 0, 0]
        };
        let trimCreateRet = await this.command.surface.trimSurfaceIncrement(params, trimSurfaceName);
        if (trimCreateRet?.code === 200 && trimCreateRet?.data?.modelData?.currentFeature?.length > 0) {
            let currentFeature = trimCreateRet?.data?.modelData?.currentFeature;
            //查询element信息(裁剪面生成的特征中无法直接查到面，需要先查element，查entity，再查到face)
            if (currentFeature.length > 0 && currentFeature[0]?.type === "TrimSurface") {
                let elementsOnFeature = await this.query.getElementsByFeatureId(currentFeature[0].id);
                if (elementsOnFeature?.code === 200 && elementsOnFeature?.data?.elements?.length > 0) {
                    let eleRefEntityId = elementsOnFeature?.data?.elements[0]?.params?.entityRefId;
                    let elementsOnEntity = await this.query.getElementsByEntityId(eleRefEntityId);
                    if (elementsOnEntity?.code === 200 && elementsOnEntity?.data?.elements?.length > 0) {
                        for (let i = 0; i < elementsOnEntity?.data?.elements.length; i++) {
                            let ele = elementsOnEntity?.data?.elements[i];
                            if (ele?.type === ElementType.FACE) {
                                faceRet.elementId = ele?.id;
                                faceRet.elementName = ele?.name;
                                faceRet.type = ele?.type;
                                faceRet.entityId = ele?.params?.entityRefId;
                                faceRet.entityName = ele?.params?.name;
                                faceRet.code = trimCreateRet?.code;
                                break;
                            }
                        }
                    }
                }
            }
        }
        faceRet.code = trimCreateRet?.code;
        return faceRet;
    }

    /**
     * 加厚成体
     * @param faceEntityId 面所属体的entityId
     * @param thickness 加厚值
     * @param outward 加厚朝向
     * @param featureName
     * @returns {Promise<void>} 返回创建体的信息
     */
    async thickenSurface(faceEntityId, thickness, outward, featureName) {
        let thickenRet = {};
        let params = {
            thickenSurfaceId: faceEntityId,
            thickness: thickness,
            outward: outward
        };
        let thickenFaceRet = await this.command.solid.thickenIncrement(params, featureName);
        if (thickenFaceRet?.code === 200) {
            let currentFeature = thickenFaceRet?.data?.modelData?.currentFeature;
            //查询element信息-需要查entity的信息
            if (currentFeature.length > 0) {
                let entityId = await this.toolQuery.getEntitiesByFeatureId(currentFeature[0].id);
                if (entityId?.code === 200) {
                    let entities = entityId?.data?.entities;
                    thickenRet.entityId = entities[0].id;
                    thickenRet.entityName = entities[0].name;
                    thickenRet.code = entityId?.code;
                }
            }
        }
        return thickenRet;
    }

    /**
     * 拉伸曲面到面
     * @param faceId 开始曲面
     * @param toSurfaceId 结束目标曲面
     * @param directionLinePoint 方向参考直线的选择起始点
     * @param featureName 特征名字
     * @param reverse 是否反向
     * @returns {Promise<{}>}
     */
    async extrudeSurface(faceId, toSurfaceId, directionLinePoint, reverse, featureName) {
        let extrudeRet = {faces: [], code: 0};
        let params = {
            curveIds: [faceId],
            toSurface1: toSurfaceId,
            direction: new Direction().setPoint(directionLinePoint?.start, directionLinePoint?.end),
            directionType: 1,
            extrudeType1: 1,
            reverse: reverse
        };
        let extrudeSurfaceRet = await this.command.surface.extrudeSurfaceIncrement(params, featureName);
        if (extrudeSurfaceRet?.code === 200) {
            let currentFeature = extrudeSurfaceRet?.data?.modelData?.currentFeature;
            //查询element信息(拉伸面生成的特征中查面)
            if (currentFeature.length > 0 && currentFeature[0]?.type === "ExtrudeSurface") {
                let elementsOnFeature = await this.query.getElementsByFeatureId(currentFeature[0].id);
                if (elementsOnFeature?.code === 200 && elementsOnFeature?.data?.elements?.length > 0) {
                    for (let i = 0; i < elementsOnFeature?.data?.elements.length; i++) {
                        let ele = elementsOnFeature?.data?.elements[i];
                        if (ele?.type === ElementType.FACE) {
                            let tempFace = {
                                elementId: ele?.id,
                                elementName: ele?.name,
                                type: ele?.type,
                                entityId: ele?.params?.entityRefId,
                                entityName: ele?.params?.name
                            };
                            extrudeRet.faces.push(tempFace);
                        }
                    }
                    if (extrudeRet?.faces?.length > 0) {
                        extrudeRet.code = extrudeSurfaceRet?.code;
                    }
                }
            }
        }
        return extrudeRet;
    }

    /**
     * 面缝合成体（缝合时需要使用面的entityid）
     * @param entityIds 面entityID数组
     * @param featureName 特征名
     * @returns {Promise<{}>}
     */
    async knitSurface(entityIds, featureName) {
        let knitRet = {};
        let params = {
            surfaces: entityIds,
            controlGap: 1
        };
        let knitSurfaceRet = await this.command.surface.knitSurfaceIncrement(params, featureName);
        if (knitSurfaceRet?.code === 200) {
            let currentFeature = knitSurfaceRet?.data?.modelData?.currentFeature;
            //查询element信息-需要查entity的信息
            if (currentFeature.length > 0) {
                let entityId = await this.toolQuery.getEntitiesByFeatureId(currentFeature[0].id);
                if (entityId?.code === 200) {
                    let entities = entityId?.data?.entities;
                    knitRet.entityId = entities[0].id;
                    knitRet.entityName = entities[0].name;
                    knitRet.code = entityId?.code;
                }
            }
        }
        return knitRet;
    }

    /**
     * 变换复制面
     * @param faceEntityId 参照面entityID
     * @param distance 距离
     * @param directionLinePoint 方向参考直线的选择起始点
     * @param featureName 特征名字
     * @param reverse 是否反向
     * @returns {Promise<{}>}
     */
    async transformCopySurface(faceEntityId, distance, directionLinePoint, reverse, featureName) {
        let transCopySurfaceRet = {};
        let params = {
            entityIds: [faceEntityId],
            copy: 1,
            distance: distance,
            direction: new Direction().setPoint(directionLinePoint?.start, directionLinePoint?.end),
            reverse: reverse
        };
        let transformRet = await this.command.solid.createTransformByDistanceIncrement(params, featureName);
        if (transformRet?.code === 200 && transformRet?.data?.modelData?.currentFeature?.length > 0) {
            let currentFeature = transformRet?.data?.modelData?.currentFeature;
            let elementsOnFeature = await this.query.getElementsByFeatureId(currentFeature[0]?.id);
            if (elementsOnFeature?.code === 200 && elementsOnFeature?.data?.elements?.length > 0) {
                for (let i = 0; i < elementsOnFeature?.data?.elements.length; i++) {
                    let ele = elementsOnFeature?.data?.elements[i];
                    if (ele?.type === ElementType.FACE) {
                        let tempFace = {
                            elementId: ele?.id,
                            elementName: ele?.name,
                            type: ele?.type,
                            entityId: ele?.params?.entityRefId,
                            entityName: ele?.params?.name
                        };
                        transCopySurfaceRet = tempFace;
                        transCopySurfaceRet.code = transformRet?.code;
                    }
                }
            }
        }
        return transCopySurfaceRet;
    }

    /**
     * 实体布尔并运算
     * @param entityIds 要计算的实体entityIds
     * @param featureName 特征名字
     * @returns {Promise<void>}
     */
    async booleanUnion(entityIds, featureName) {
        let booleanUnionRet = {};
        let params = {
            entitys: entityIds
        };
        let unionRet = await this.command.solid.booleanUnionIncrement(params, featureName);
        let currentFeature = unionRet?.data?.modelData?.currentFeature;
        if (unionRet?.code === 200 && currentFeature?.length > 0) {
            let entitiesOnFeature = await this.query.getEntitiesByFeatureIds([currentFeature[0]?.id]);
            if (entitiesOnFeature?.code === 200 && entitiesOnFeature?.data?.entities?.length > 0) {
                let tempEntity = entitiesOnFeature?.data?.entities[0];
                booleanUnionRet.entityId = tempEntity?.id;
                booleanUnionRet.entityName = tempEntity?.name;
                booleanUnionRet.type = tempEntity?.type;
                booleanUnionRet.code = entitiesOnFeature.code;
            }
        }
        return booleanUnionRet;
    }

    /**
     * 生成特征名字
     * @param type 类型，值有solid、plane、copy、trim、transform、extrude、knit等
     */
    generateFeatrueName(type) {
        let featureRet = "";
        return featureRet;
        switch (type) {
            case "solid":
                featureRet = "实体" + this.solidIndex.toString();
                this.solidIndex++;
                break;
            case "plane":
                featureRet = "基准面" + this.planeIndex.toString();
                this.planeIndex++;
                break;
            case "copy":
                featureRet = "复制曲面" + this.copyFaceIndex.toString();
                this.copyFaceIndex++;
                break;
            case "trim":
                featureRet = "裁剪曲面" + this.trimSurfaceIndex.toString();
                this.trimSurfaceIndex++;
                break;
            case "transform":
                featureRet = "变换" + this.transformSurfaceIndex.toString();
                this.transformSurfaceIndex++;
                break;
            case "extrude":
                featureRet = "拉伸曲面" + this.extrudeFaceIndex.toString();
                this.extrudeFaceIndex++;
                break;
            case "knit":
                featureRet = "缝合曲面" + this.knitSurfaceIndex.toString();
                this.knitSurfaceIndex++;
                break;
            default:
                break;
        }
        return "wingbeam" + featureRet;
    }
}

const wingBeamCommand = new WingBeamCommand();
export {wingBeamCommand};